package cn.tedu.mall.seckill.mappers;

import cn.tedu.mall.pojo.seckill.model.SeckillStockLog;
import cn.tedu.mall.pojo.seckill.vo.SeckillSpuVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SeckillSpuMapper {
    @Select("select * from seckill_spu")
    List<SeckillSpuVO> selectAll();

    @Select("select * from seckill_stock_log where order_sn=#{sn}")
    SeckillStockLog selectStockLogBySn(@Param("sn") String sn);

    @Update("UPDATE seckill_sku SET stock = stock - #{quantity},gmt_modified = now() WHERE id = #{skuId} AND stock > 0")
    int decrStock(@Param("skuId") Long skuId, @Param("quantity") Integer quantity);

    @Update("UPDATE seckill_sku SET stock = stock + #{quantity},gmt_modified = now() WHERE id = #{skuId}")
    void incrStock(@Param("skuId") Long skuId, @Param("quantity") Integer quantity);

    @Insert("INSERT INTO seckill_stock_log (sku_id,order_sn,quantity,gmt_create,gmt_modified) VALUES (#{skuId},#{orderSn},#{quantity},now(),now())")
    void insertStockLog(SeckillStockLog stockLog);
}
